package com.dc.datacollect.controller;

import com.dc.datacollect.conduit.R;
import com.dc.datacollect.dto.input.RenameGroupsInput;
import com.dc.datacollect.entity.Groups;
import com.dc.datacollect.service.GroupsService;
import com.dc.datacollect.helpers.SessionProvide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/groups")
public class GroupsController {
    @Autowired
    private GroupsService groupsService;
    @PostMapping("/add/{groupsName}")
    public R createGroups(@PathVariable("groupsName") String groupsName) throws Exception {
        boolean isCreateSuccess = groupsService.createGroups(groupsName);
        return R.auto("创建集合",isCreateSuccess);
    }
    @GetMapping ("/getGroupsTree")
    public R getGroupsTree() {
        Long collectorId = SessionProvide.getLoginObject().getId();
        List<Groups> groupsTree =  groupsService.getGroupsTreeByCollectorId(collectorId);
        return R.ok("获取集合树成功").data(groupsTree);
    }
    @PostMapping("/remove/{groupsId}")
    public R remove(@NotNull @PathVariable("groupsId") Long groupsId) {
        return R.auto("删除集合", this.groupsService.removeGroupsById(groupsId));
    }
    @PostMapping("/rename")
    public R rename(@Valid @RequestBody RenameGroupsInput renameGroupsInput) throws Exception {
        Boolean isSuccess = groupsService.rename(renameGroupsInput);
        return R.auto("重命名组",isSuccess);
    }
}
